package Decorator;

import java.awt.Graphics;
import java.awt.Image;
import java.io.File;

import javax.swing.JPanel;

import pyrachnid.model.robot_model.Robot;

public class RobotDecorator extends DecoratorAnimated {
	
	private Robot _robot;
	private MapBase _mapBase;
	
	public RobotDecorator(JPanel p_panneau, Item p_item, Robot p_robot, MapBase p_mapBase) {
		super(p_panneau, p_item, new File(System.getProperty("user.dir") + "/bin" + p_robot.getRobotType().getImagePath()));
		_robot = p_robot;
		_mapBase = p_mapBase;
	}
	
	public void paintComponent(Graphics g) {
		//On invoque la m�thode de la classe Decorator
		super.paintComponent(g);

		int y = 0;
		int x = 0;
		int positionX = 0;
		int positionY = 0;
		int tailleRobot = 0;
		float echelle = ((float) _mapBase.parent.getWidth() / (float) _mapBase._imageWidth);
		
		if (this.parent.getWidth() > _mapBase._scaledImage.getWidth()) {
			x = _mapBase._scaledImage.getWidth() / _mapBase._XNumber;
		} else {
			x = this.parent.getWidth() / _mapBase._XNumber;
		}
		if (this.parent.getHeight() > _mapBase._scaledImage.getHeight()) {
			y = _mapBase._scaledImage.getHeight() / _mapBase._YNumber;
		} else {
			y = this.parent.getHeight() / _mapBase._YNumber;
		}
		
		positionX = (int) (_robot.getRealPosition().x * echelle - x / 4);
		positionY = (int) (_robot.getRealPosition().y * echelle - y / 2);
		
		tailleRobot = (x < y)? x : y;
		Image img = (_robot.getIsMoove() && getIsAnnimated())? getAnnimateImage() : getStaticImage();
		g.drawImage(img, positionX, positionY, tailleRobot, tailleRobot, this.parent);
	}
}
